<?php

/**
 *    商品分享管理控制器
 *
 *    @author    Hyber
 *    @usage    none
 */
class DictionaryApp extends BackendApp
{
	var $_m_dictionary;

	function __construct()
	{
		$this->DictionaryApp();
	}

	function DictionaryApp()
	{
		parent::BackendApp();

		$this->_m_dictionary = &m('dictionary');
	}

	/**
	 *    商品分享索引
	 *
	 *    @author    Hyber
	 *    @return    void
	 */
	function index()
	{
		$search_options = array(
            'd_key'   => Lang::get('key'),
            'd_code'   => Lang::get('code'),
		);
		/* 默认搜索的字段是店铺名 */
		$field = 'd_key';
		array_key_exists(@$_GET['field'], $search_options) && $field = @$_GET['field'];
		$conditions = $this->_get_query_conditions(array(array(
            'field' => $field,       //按用户名,店铺名,支付方式名称进行搜索
            'equal' => 'LIKE',
            'name'  => 'search_name',
		),
		));

		$page   =   $this->_get_page(10);    //获取分页信息
		$this->assign('filtered', $conditions? 1 : 0); //是否有查询条件
		$conditions.=" and d_code='boxtype'";
		$dictionarys = $this->_m_dictionary->find(array(
            'fields'   => '*',
            'conditions'    => '1=1 '.$conditions.'order by d_tid desc',
            'limit'   => $page['limit'],
		//'join'    => 'belongs_to_acategory',
		//'order'   => 'article.sort_order ASC,article.add_time DESC', //必须加别名
            'count'   => true   //允许统计
		));
		$page['item_count'] = $this->_m_dictionary->getCount();   //获取统计的数据
		$this->import_resource(array('script' => 'inline_edit.js'));
		//var_dump($page['item_count']);
		$this->_format_page($page);

		$this->assign('search_options', $search_options);
		$this->assign('page_info', $page);          //将分页信息传递给视图，用于形成分页条
		$this->assign('dictionarys', $dictionarys);
		$this->assign('tabself', json_encode(array('text'=>'字典管理')));
		$this->display('dictionary.index.html');
	}

	function add()
	{
		if (!IS_POST)
		{
			$this->import_resource('jquery.plugins/jquery.validate.js');
			$this->assign('dictionary', array('d_order' => 1, 'd_del' => 0));
			$this->assign('type', $this->_get_dictionary_type());
			$this->assign('tabself', json_encode(array('text'=>'新增字典')));
            $this->assign('tabback', json_encode(array('text'=>'字典管理','url'=>'')));
		
			$this->display('dictionary.form.html');
		}
		else
		{
			$data = array(
                'd_key' => trim($_POST['key']),
                'd_code'  => trim($_POST['code']),
                'd_del'  => intval($_POST['type']),
                'd_value'  => trim($_POST['value']),
                'd_order' => intval($_POST['order']),
			);

			if (!$dictionary_id = $this->_m_dictionary->add($data))
			{
				$this->show_warning($this->_m_dictionary->get_error());
				return;
			}


			$this->show_message('add_dictionary_successed',
                '重新编辑', 'index.php?app=dictionary&act=edit&id='.$dictionary_id
                );
		}
	}

	function edit()
	{
		$dictionary_id = empty($_GET['id']) ? 0 : $_GET['id'];
		if (!$dictionary_id)
		{
			$this->show_warning('no_such_dictionary');
			return;
		}
		$dictionary = $this->_m_dictionary->find(array(
            'fields'   => '*',
            'conditions'  => 'd_tid='.$dictionary_id.'',
		//'limit'   => $page['limit'],
		//'join'    => 'belongs_to_acategory',
		//'order'   => 'article.sort_order ASC,article.add_time DESC', //必须加别名
            'count'   => true   //允许统计
		));
		if (!$dictionary)
		{
			$this->show_warning('no_such_dictionary');
			return;
		}
		if (!IS_POST)
		{
			$this->import_resource('jquery.plugins/jquery.validate.js');
			$data['d_tid'] = $dictionary[$dictionary_id]['d_tid'];
			$data['d_key'] = $dictionary[$dictionary_id]['d_key'];
			$data['d_code'] = $dictionary[$dictionary_id]['d_code'];
			$data['d_value'] = $dictionary[$dictionary_id]['d_value'];
			$data['d_del'] = $dictionary[$dictionary_id]['d_del'];
			$data['d_order'] = $dictionary[$dictionary_id]['d_order'];

			$this->assign('dictionary', $data);
			$this->assign('type', $this->_get_dictionary_type());
			$this->assign('tabself', json_encode(array('text'=>'编辑字典')));
            $this->assign('tabback', json_encode(array('text'=>'字典管理','url'=>'')));
		
			$this->display('dictionary.form.html');
		}
		else
		{
			$data = array(
                'd_code' => trim($_POST['code']),
                'd_key'  => trim($_POST['key']),
                'd_value'  => trim($_POST['value']),
                'd_del' => intval($_POST['type']),
                'd_order' => intval($_POST['order']),
			);
 
			 
		 
			
				$rows=$this->_m_dictionary->edit($dictionary_id,$data);
			if ($this->_m_dictionary->has_error())
			{
				$this->show_warning('修改失败');
				return;
			}	
			

			$this->show_message('edit_dictionary_successed',
                'edit_again', 'index.php?app=dictionary&act=edit&id=' . $dictionary_id);
		}
	}

	function drop()
	{
		$dictionary_ids = isset($_GET['id']) ? trim($_GET['id']) : 0;
		if (!$dictionary_ids)
		{
			$this->show_warning('no_such_dictionary');

			return;
		}
		$dictionary_ids = explode(',', $dictionary_ids);//获取一个类似array(1, 2, 3)的数组
		foreach ($dictionary_ids as $dictionary_id)
		{
			$this->_m_dictionary->drop($dictionary_id);
		}
		$this->_clear_cache();
		$this->show_message('drop_dictionary_successed');
	}

	//异步修改数据
	function ajax_col() 
	{
		$id     = empty($_GET['id']) ? 0 : intval($_GET['id']);
		$column = empty($_GET['column']) ? '' : trim($_GET['column']);
		$value  = isset($_GET['value']) ? trim($_GET['value']) : '';
		$data   = array();

		if (in_array($column ,array('d_key','d_value','d_order','d_del')))
		{
			$data[$column] = $value;
			$this->_m_dictionary->edit($id, $data);
			if(!$this->_m_dictionary->has_error())
			{
				echo ecm_json_encode(true);
			}
		}
		else
		{
			return ;
		}
		return ;
	}

	function _get_dictionary_type()
	{
		return array(
            '0' => Lang::get('no'),
            '1'   => Lang::get('yes'),
        	'2'   => Lang::get('other'),
		);
	}

}